<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/8 15:53
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class SocialRelationShip_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $userid
     * @param int $level
     * @return mixed
     */
    public function getFansListsByUserId($userid, $level = 1, $offset, $limit = 20)
    {
        if ($level == 1) {
            $where['puserid'] = $userid;
        } else {
            $where['ppuserid'] = $userid;
        }

        $db = clone($this->db);
        $q = $db->where($where)
            ->select('user.id, u.nickname, u.headimgurl, u.created, user.amount, user.score')
            ->from('user_wx_relationship as r')
            ->join('user_wx as u', 'u.userid = r.userid')
            ->join('user', 'user.id = u.userid')
            ->limit($limit, $offset)
            ->get();

        $rows = $q->result_array();
        $data = [];
        if (!empty($rows) && is_array($rows)) foreach($rows as $item) {
            $amount = intval($item['amount']);
            $data[] = [
                'userId' => intval($item['id']),
                'nickname' => value($item, 'nickname', ''),
                'headimgurl' => value($item, 'headimgurl', ''),
                'created' => value($item, 'created', ''),
                'score' => intval($item['score']),
                'amount' => $amount,
                'amount_cny' => money_format('%i', $amount / 100)
            ];
        }

        return $data;
    }

    /**
     * 一级粉丝数量
     * @param $userid
     */
    public function getFansCountByUserId_Level_1($userid)
    {
        $where['puserid'] = $userid;
        $db = clone($this->db);
        $db->where($where);
        $db->from('user_wx_relationship');
        return $db->count_all_results();
    }

    /**
     * 二级粉丝数量
     * @param $userid
     */
    public function getFansCountByUserId_Level_2($userid)
    {
        $where['ppuserid'] = $userid;
        $db = clone($this->db);
        $db->where($where);
        $db->from('user_wx_relationship');
        return $db->count_all_results();
    }
}
